﻿using UnityEngine;
using System;
using UnityEngine.Rendering;

namespace Yoozoo.Mars.Got
{
    public enum QualityEnum
    {
        None = -1,
        Low = 0,
        Medium = 1,
        High = 2,
        VeryHigh = 3,
        Perfect = 4,
        PC = 5,
        Test = 6,
    }

    public class DeviceQuality
    {
        public QualityEnum m_currentQuality = QualityEnum.None;
        public bool m_commanderHeadShow = true;
        private Action<QualityEnum> m_changeQualityAction;
        private Action<bool> m_changeCommanderHeadAction;
        public float ZoomScale { get; set; } = 1.0f;
        private static DeviceQuality instance;
        
        public static DeviceQuality Instance
        {
            get
            {
                if (instance == null)
                {
                    instance = new DeviceQuality();
                }

                return instance;
            }
        }

        public DeviceQuality()
        {
            
        }

        public void SetCurrentQuality(QualityEnum eQuality)
        {
            m_currentQuality = eQuality;
        }

        public QualityEnum GetCurrentQuality()
        {
            return m_currentQuality;
        }

        public void RegisterAction(Action<QualityEnum> aAction)
        {
            m_changeQualityAction += aAction;
        }
        
        public void UnRegisterAction(Action<QualityEnum> aAction)
        {
            m_changeQualityAction -= aAction;
        }

        public void ChangeQuality(QualityEnum quality)
        {
            DevicesGrading.Instance.SaveQuality2PlayerPrefs((int)quality);
            m_currentQuality = quality;
            if (m_changeQualityAction != null)
            {
                m_changeQualityAction(m_currentQuality);
            }
        }

        public void ChangeCommanderHeadShow(bool isShowHead)
        {
            m_commanderHeadShow = isShowHead;
            if (m_changeCommanderHeadAction != null)
            {
                m_changeCommanderHeadAction(m_commanderHeadShow);
            }
        }

        public void RegisterCommanderHeadAction(Action<bool> aAction)
        {
            m_changeCommanderHeadAction += aAction;
        }

        public void UnRegisterCommanderHeadAction(Action<bool> aAction)
        {
            m_changeCommanderHeadAction -= aAction;
        }

        public void ClearAllActions()
        {
#if UNITY_EDITOR
            if (m_changeQualityAction != null)
            {
                var delegates = m_changeQualityAction.GetInvocationList();
                for (int i = 0; i < delegates.Length; i++)
                {
                    m_changeQualityAction -= (Action<QualityEnum>)delegates[i];
                }
            }
            
            if (m_changeCommanderHeadAction != null)
            {
                var delegates = m_changeCommanderHeadAction.GetInvocationList();
                for (int i = 0; i < delegates.Length; i++)
                {
                    m_changeCommanderHeadAction -= (Action<bool>)delegates[i];
                }
            }
            
            m_currentQuality = QualityEnum.None;
            m_commanderHeadShow = true;
            ZoomScale = 1.0f;
#endif
        }
    }
}
